﻿Imports Microsoft.DirectX.DirectSound
Imports Microsoft.DirectX.AudioVideoPlayback
''' <summary>
''' Diese Klasse sorgt fuer das abspielen von Musik.
''' </summary>
''' <remarks></remarks>
Public Class MusikEngine
    'DirectSound Objekte
    Private Shared DSoundDevice As Microsoft.DirectX.DirectSound.Device = Nothing
    Private Shared DSSecondaryBufferLocker As New Mutex
    'Fuer wav Dateien
    Private Shared DSSecondaryBuffer As New List(Of SecondaryBuffer)

    Private Shared backmusiclocker As New Mutex
    'Fuer mp3 Dateien als die Musik
    Private Shared backmusic As Audio
    Private Shared d As BufferDescription = Nothing
    Public Shared Sub LoadDevice(ByVal Form As System.IntPtr)
        'DirectSoundDevice erzeugen
        DSoundDevice = New Device
        'CooperativeLevel setzen
        DSoundDevice.SetCooperativeLevel(Form, CooperativeLevel.Priority)
        LoadBuffer()



    End Sub
    Public Shared Sub LoadBuffer()
        d = New BufferDescription
        d.ControlPan = True
        d.ControlVolume = True
        d.ControlFrequency = True
        d.ControlEffects = True

    End Sub
#Region "Sound"
    ''' <summary>
    ''' Laedt eine Wav Datei und spielt sie dann ab.
    ''' </summary>
    ''' <param name="Datei"></param>
    ''' <param name="LoopMusik"></param>
    ''' <remarks></remarks>
    Public Shared Sub LoadWav(ByVal Datei As String, ByVal LoopMusik As Boolean)
        If Not Hauptmenue.Aufzeichnungsspulen Then
            If DataSettings.MusikSound = True Then
                If My.Computer.FileSystem.FileExists(Datei) Then
                    'WAV-Datei ohne irgendwelche besonderen Flags laden
                    DSSecondaryBufferLocker.Lock()
                    Dim A As SecondaryBuffer
                    A = New SecondaryBuffer(Datei, d, DSoundDevice)
                    DSSecondaryBuffer.Add(A)
                    PlayWavMusik(LoopMusik, DSSecondaryBuffer.Count - 1)
                    DSSecondaryBufferLocker.Unlock()
                End If
            End If
        End If
    End Sub
    ''' <summary>
    ''' Spielt eine Wav Datei ab
    ''' </summary>
    ''' <param name="LoopMusik"></param>
    ''' <param name="Buffer"></param>
    ''' <remarks></remarks>
    Public Shared Sub PlayWavMusik(ByVal LoopMusik As Boolean, ByVal Buffer As Integer)
        If (Not DSoundDevice Is Nothing) Then
            'Abspielen der WAV-Datei
            'Je nachdem, ob die CheckBox True oder False ist,
            'also das WAV sich wiederholen soll oder nicht
            If LoopMusik = True Then
                DSSecondaryBuffer(Buffer).Play(0, BufferPlayFlags.Looping)
            Else
                DSSecondaryBuffer(Buffer).Play(0, BufferPlayFlags.Default)
            End If
        End If
    End Sub
    ''' <summary>
    ''' Frames fuer die Wav Musik
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub FrameMusik()
        Dim I As Integer = 0
        'Wav Dateien loeschen
        DSSecondaryBufferLocker.Lock()
        While I < DSSecondaryBuffer.Count - 1
            If DSSecondaryBuffer(I).Status.Playing = False Then
                DSSecondaryBuffer(I).Dispose()
                DSSecondaryBuffer(I) = Nothing
                DSSecondaryBuffer.RemoveAt(I)
            Else
                I += 1
            End If
        End While
        DSSecondaryBufferLocker.Unlock()
    End Sub
#End Region
#Region "Musik"
    ''' <summary>
    ''' Laedt die Musik und spielt sie dann ab
    ''' </summary>
    ''' <param name="Datei"></param>
    ''' <remarks></remarks>
    Public Shared Sub LoadMusik(ByVal Datei As String)
        If My.Computer.FileSystem.FileExists(Datei) Then
            If DataSettings.MusikSound = True Then
                If DataSettings.Hintergrundmusik And Datei.ToLower.EndsWith(".mp3") Then
                    If Not backmusic Is Nothing Then
                        Dim Terminated As Boolean = False
                        While Not Terminated
                            Try
                                backmusic.Stop()
                                backmusic.Dispose()
                                backmusic = Nothing
                                Terminated = True
                            Catch
                            End Try
                        End While
                    End If


                    backmusic = New Audio(Datei)
                    backmusic.Play()
                End If
            End If
        End If
    End Sub
    Public Shared Function MusikStopped() As Boolean
        If Not backmusic Is Nothing Then
            If backmusic.Stopped Then
                Return True
            Else
                Return False
            End If
        Else
            'Wir haben keine Musik das bedeutet wir brauchen eine.
            Return True
        End If
    End Function
    Public Shared Function MusikPaused() As Boolean
        If Not backmusic Is Nothing Then
            If backmusic.Paused Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function
    Public Shared Sub MusikStart()
        backmusic.CurrentPosition = 0
        backmusic.Play()
    End Sub
    Public Shared Sub RestartMusik()

        backmusic.Play()
    End Sub
    Public Shared Sub Pause(Optional ByVal Pausieren As Boolean = True)
        If Pausieren = True Then
            backmusic.Pause()
        Else
            backmusic.Play()
        End If
    End Sub
    Public Shared Sub MusikStop()
        If Not backmusic Is Nothing Then
            backmusic.Stop()
        End If
    End Sub
    Public Shared Function MusikEnded() As Boolean
        If Not backmusic Is Nothing Then
            While True
                Try
                    If backmusic.Duration = backmusic.CurrentPosition Or backmusic.CurrentPosition = 0 And backmusic.State <> StateFlags.Running Then
                        Return True
                    Else
                        Return False
                    End If
                Catch
                End Try
            End While

        Else
            Return True
        End If
    End Function
    ''' <summary>
    ''' Immediatly release all Data hold by the Musikengine
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub Dispose()
        DSSecondaryBufferLocker.Lock()
        For I = 0 To DSSecondaryBuffer.Count - 1
            DSSecondaryBuffer(I).Dispose()
        Next
        DSSecondaryBuffer.Clear()
        DSSecondaryBufferLocker.Unlock()
        If Not backmusic Is Nothing Then
            backmusiclocker.Lock()
            backmusic.Dispose()
            backmusic = Nothing
            backmusiclocker.Unlock()
        End If
    End Sub
#End Region
End Class
